var allData = [
  {name: "北京",id:74},
  {name: "天津",id:75},
  {name: "上海",id:81},
  {name: "重庆",id:104},
  {name: "河北",id:78},
  {name: "河南",id:88},
  {name: "云南",id:105},
  {name: "辽宁",id:73},
  {name: "黑龙江",id:71},
  {name: "湖南",id:90},
  {name: "安徽",id:84},
  {name: "山东",id:76},
  {name: "新疆",},
  {name: "江苏",},
  {name: "浙江",},
  {name: "江西",},
  {name: "湖北",},
  {name: "广西",},
  {name: "甘肃",},
  {name: "山西",},
  {name: "内蒙古",},
  {name: "陕西",},
  {name: "吉林",},
  {name: "福建",},
  {name: "贵州",},
  {name: "广东",},
  {name: "青海",},
  {name: "西藏",},
  {name: "四川",},
  {name: "宁夏",},
  {name: "海南",},
  {name: "台湾",},
  {name: "香港",},
  {name: "澳门",},
];
var myChart2 = echarts.init(document.getElementById('all'));
var laytpl   = '';
var THHIS_PAGE_TYPE = 'hm_index';   //  页面配置标识(具体通过管理后台查看)


var INDEX = {
	
	//  初始化准备
	initial:function(){
		layui.use(function(){
		  laytpl = layui.laytpl;
		  
		});
		
		INDEX.getHomeCountries();
		INDEX.getHomeProvinces(16);
		INDEX.getHomeStorages();
		INDEX.getHomeSubs(8);
		INDEX.getHomeSubs(9);
		INDEX.getHomeSubs(10);
		
		
		// 制造业产业、技术品牌库 菜单跳转
		$('#view_storages').on('click','.list_li',function(){
			var id = $(this).attr('data-id')  
			window.location.href = './home/component/index.html?id='+id;
		})
		
		
		//  国家库 省市库 详情跳转
		$('.tem_mks').on('click','.tds',function(){
			var id   = $(this).attr('data-id');  
			var type = $(this).attr('data-type');  
			localStorage.setItem("brand_category_id", id);
			if(type == 1){
				window.location.href = './country/html/index.html?id='+id;
			}else{
				window.location.href = './district/html/index.html?id='+id;
			}
		})
		
		// 制造业专区品牌库 商业辅助品牌库 等详情跳转
		$('.right_more_box').on('click','.r_m_lis',function(){
			var id   = $(this).attr('data-id');  
			var type = $(this).attr('data-type');  
			localStorage.setItem("brand_category_id", id);
			
			switch (type*1){
				case 1:
					window.location.href = './manufacturing/html/index.html?id='+id;
					break;
				case 2:
					window.location.href = './auxiliary/html/index.html?id='+id;
					break;
				case 3:
					window.location.href = './ppt/html/index.html?id='+id;
					break;	
			}
		})
		
		// 控制 办公辅助品牌库 显示
		$('.right_more_box').on('click','#moreBtnList',function(){
			layer.open({
			  type: 1,
			  title: ['办公辅助品牌库', 'font-size:16px;font-weight: 600'],
			  area: ['800px', '620px'],
			  shade: true, // 显示遮罩
			  content: $('#Offices'), // 捕获的元素
			  end: function(){
				// layer.msg('关闭后的回调', {icon:6});
			  }
			});
			
		})
		
		
		
		
		
	},
	
	// 中国地图需要数据 组装
	chinaDataJion(v){
		var list = [];
		if(v.code == 1){
			var m = v.data;
			for (var i = 0; i < m.length; i++) {
				if(m[i].children){
					var c = m[i].children;
					for (var k = 0; k < c.length; k++) {
						list.push(c[k])
					}
				}
			}
		}
		console.log(list)
	},
	
	
	//  获取 制造业各省市品牌库
	getHomeProvinces(v){
		
		// 先获取一下本地缓存
		var jingjiang_provinces_data = localStorage.getItem("jingjiang_provinces_data")
		if(jingjiang_provinces_data){
			try{
				var d = JSON.parse(jingjiang_provinces_data)
				var hrml4 = template('script_provinces', d);
				$("#view_provinces").empty().append(hrml4); 
				
				INDEX.chinaDataJion(d)
				
			}catch(e){
				
			}
		} 
		
		var datas = {
		 	url  :'/api/region/provinces', 
		 	http_type:'POST',
			datas:{
				country_id:v
			}
		}
		wxx.HttpReqeust(datas,function(ret){
		 	if(ret.code == 1){
		 		var hrml4 = template('script_provinces', ret);
		 		$("#view_provinces").empty().append(hrml4); 
				
				var hrml6 = template('script_provinces2', ret);
				$("#view_provinces2").empty().append(hrml6); 
				
				var hrml7 = template('script_provinces3', ret);
				$("#view_provinces3").empty().append(hrml7); 
				
				INDEX.chinaDataJion(ret)
				
				// 存储在本地 每次请求先获取 本地信息
				localStorage.setItem("jingjiang_provinces_data", JSON.stringify(ret));
				
		 	}
		});
	},
	
	//  获取 制造业各国家品牌库
	getHomeCountries:function(){
		
		// 先获取一下本地缓存
		var jingjiang_countries_data = localStorage.getItem("jingjiang_countries_data")
		if(jingjiang_countries_data){
			try{
				var d = JSON.parse(jingjiang_countries_data)
				var hrml3 = template('script_countries', d);
				$("#view_countries").empty().append(hrml3); 
				
			}catch(e){
				
			}
		} 
		
		var datas = {
		 	url  :'/api/region/countries', 
		 	http_type:'POST',  
		}
		wxx.HttpReqeust(datas,function(ret){
		 	if(ret.code == 1){
		 		var hrml3 = template('script_countries', ret);
		 		$("#view_countries").empty().append(hrml3); 
				
				var hrml4 = template('script_countries2', ret);
		 		$("#view_countries2").empty().append(hrml4); 
				
				var hrml5 = template('script_countries3', ret);
				$("#view_countries3").empty().append(hrml5); 
				
				// 存储在本地 每次请求先获取 本地信息
				localStorage.setItem("jingjiang_countries_data", JSON.stringify(ret));
				
				
		 	}
		});
	},
	
	//  获取 制造业专区品牌库
	 getHomeSubs:function(v){
		 
		// 先获取一下本地缓存
		var jingjiang_subs_data = localStorage.getItem("jingjiang_subs_data"+v)
		if(jingjiang_subs_data){
			try{
				var d = JSON.parse(jingjiang_subs_data)
				var hrml2 = template('script_subs'+v, d);
				$("#view_subs"+v).empty().append(hrml2); 
				
			}catch(e){
				
			}
		} 
		 
		var datas = {
		 	url  :'/api/brand_category/subs',
		 	http_type:'POST',  
		 	datas:{parent_id:v}
		}
		wxx.HttpReqeust(datas,function(ret){
		 	if(ret.code == 1){
		 		var hrml2 = template('script_subs'+v, ret.data);
		 		$("#view_subs"+v).empty().append(hrml2); 
				
				// 存储在本地 每次请求先获取 本地信息
				localStorage.setItem("jingjiang_subs_data"+v, JSON.stringify(ret.data));
				
		 	}
		});
	 },
	
	
	//  获取 制造业产业、技术品牌库
	getHomeStorages:function(){
		
		// 先获取一下本地缓存 
		var jingjiang_storages_data = localStorage.getItem("jingjiang_storages_data")
		if(jingjiang_storages_data){
			try{
				var d = JSON.parse(jingjiang_storages_data)
				var hrml1 = template('script_storages', d);
				$("#view_storages").empty().append(hrml1); 
				
			}catch(e){
				
			}
		}
		
		var datas = {
			url  :'/api/brand_category/storages',
			http_type:'POST',  
			datas:{}
		}
		wxx.HttpReqeust(datas,function(ret){
			if(ret.code == 1){
				var hrml1 = template('script_storages', ret.data);
				$("#view_storages").empty().append(hrml1);
				
				// 存储在本地 每次请求先获取 本地信息
				localStorage.setItem("jingjiang_storages_data", JSON.stringify(ret.data));
				
			}
		});
	},
	
	
	
	
	//  中国地图
	chinaFun:function(){

		var myChart = echarts.init(document.getElementById('all'));
		//各省份的地图json文件
		var provinces = {
		  上海: "./static/json/310000.json",
		  河北: "./static/json/130000.json", 
		  山西: "./static/json/140000.json",
		  内蒙古: "./static/json/150000.json",
		  辽宁: "./static/json/210000.json",
		  吉林: "./static/json/220000.json",
		  黑龙江: "./static/json/230000.json",
		  江苏: "./static/json/320000.json",
		  浙江: "./static/json/330000.json",
		  安徽: "./static/json/340000.json",
		  福建: "./static/json/350000.json",
		  江西: "./static/json/360000.json",
		  山东: "./static/json/370000.json",
		  河南: "./static/json/410000.json",
		  湖北: "./static/json/420000.json",
		  湖南: "./static/json/430000.json",
		  广东: "./static/json/440000.json",
		  广西: "./static/json/450000.json",
		  海南: "./static/json/460000.json",
		  四川: "./static/json/510000.json",
		  贵州: "./static/json/520000.json",
		  云南: "./static/json/530000.json",
		  西藏: "./static/json/540000.json",
		  陕西: "./static/json/610000.json",
		  甘肃: "./static/json/620000.json",
		  青海: "./static/json/630000.json",
		  宁夏: "./static/json/640000.json",
		  新疆: "./static/json/650000.json",
		  北京: "./static/json/110000.json",
		  天津: "./static/json/120000.json",
		  重庆: "./static/json/500000.json",
		  香港: "./static/json/810000.json",
		  澳门: "./static/json/820000.json",
		};
		
	
		for (var i = 0; i < allData.length; i++) {
		  allData[i].value = Math.round(Math.random() * 200);
		}
		
		INDEX.loadMap("./static/json/all.json", "china"); //初始化全国地图
		
		var timeFn = null;
		
		//单击切换到省级地图，当mapCode有值,说明可以切换到下级地图
		myChart2.on("click", function (params) {
		  clearTimeout(timeFn);
		  //由于单击事件和双击事件冲突，故单击的响应事件延迟250毫秒执行
		  timeFn = setTimeout(function () {
		    var name = params.name; //地区name
		    var mapCode = provinces[name]; //地区的json数据
		    if (!mapCode) {
		      alert("无此区域地图显示");
		      return;
		    }
		
		    INDEX.loadMap(mapCode, name);
		  }, 250);
		});
		
		// 绑定双击事件，返回全国地图
		myChart2.on("dblclick", function (params) {
		  //当双击事件发生时，清除单击事件，仅响应双击事件
		  clearTimeout(timeFn);
		
		  //返回全国地图
		  INDEX.loadMap("./static/json/all.json", "china");
		});
		
		
	
	},
	
	/**
	 获取对应的json地图数据，然后向echarts注册该区域的地图，最后加载地图信息
	 @params {String} mapCode:json数据的地址
	 @params {String} name: 地图名称
	 */
	loadMap:function(mapCode, name) {
	  $.get(mapCode, function (data) {
		if (data) {
		  echarts.registerMap(name, data);
		  var option = {
			tooltip: {
			  show: true,
			  formatter: function (params) {
				if (params.data) return params.name + "-品牌库：" + params.data["value"];
			  },
			},
			
			visualMap: {
			    show: true,
			    left: 'left',
			    top: 'bottom',
			    seriesIndex: [0],
			    type:'piecewise',
			    pieces:[
			        {min:100,color: '#ef385d'},
			        {min:40, max:100, color: '#f5a71d'},
			        {min:20, max:40, color: '#2fb1c0'},
			        {min:5, max:20, color: '#b1d236'},
			        {min:1, max:5, color: 'rgb(131,175,155)'}
			    ],            
			    textStyle: {
			        color: '#000000'
			    }
			},  
			
			// visualMap: {
			//   type: "continuous",
			//   text: ["", ""],
			//   showLabel: true,
			//   left: "50",
			//   min: 0,
			//   max: 100,
			//   inRange: {
			// 	color: ["#edfbfb", "#b7d6f3", "#40a9ed", "#3598c1", "#215096"],
			//   },
			//   splitNumber: 0,
			// },
			series: [
			  {
				name: "MAP",
				type: "map",
				mapType: name,
				selectedMode: "false", //是否允许选中多个区域
				label: {
				  normal: {
					show: true,
				  },
				  emphasis: {
					show: true,
				  },
				},
				zoom:1.1,//地图放大倍数
				roam: false, //  缩放
				data: allData,
				itemStyle: {
				    normal: {
				        areaColor: '#F6F6F6',
				        borderColor: '#666666',
				    },
				    emphasis: {
				        areaColor: '#C10D26',
				    }
				}
			  },
			],
		  };
		  myChart2.setOption(option);
		  // curMap = {
		  //     mapCode: mapCode,
		  //     mapName: name
		  // };
		} else {
		  alert("无法加载该地图");
		}
	  });
	},
	
	
	
	//  世界地图
	wordFun:function(){
		// 基于准备好的dom，初始化echarts实例
		var myChart = echarts.init(document.getElementById('main'));
			
		// 指定图表的配置项和数据
		var name_title = "制造业各国家品牌库"
		var mapName = 'world'
		var data = [
			{ name: 'China', value: 17252 },
			{ name: 'Japan', value: 20 },
			{ name: 'Thailand', value: 19 },
			{ name: 'Singapore', value: 18 },
			{ name: 'Korea', value: 15 },
			{ name: 'Australia', value: 10 },
			{ name: 'United States', value: 50 },
			{ name: 'Germany', value: 10 },
			{ name: 'Malaysia', value: 8 },
			{ name: 'Vietnam', value: 7 },
			{ name: 'France', value: 6 },
			{ name: 'United Arab Emirates', value: 50 },
			{ name: 'Canada', value: 30 },
			{ name: 'India', value: 3 },
			{ name: 'Philippines', value: 2 },
			{ name: 'Italy', value: 2 },
			{ name: 'United Kingdom', value: 2 },
			{ name: 'Russia', value: 70 },
			{ name: 'Nepal', value: 1 },
			{ name: 'Sri Lanka', value: 1 },
			{ name: 'Finland', value: 1 },
			{ name: 'Sweden', value: 1 },
			{ name: 'Cambodia', value: 1 },
		];
		
		var option = {
		    title: {
		        text: '',
		        x: 'center',
		        textStyle: {
		            fontSize: 24
		        },                
		    },
		    tooltip: {
		        trigger: 'item',
		        formatter: function(params) {                        
		                var toolTiphtml = ''
						if (isNaN(params.value)){
							toolTiphtml = params.name + '品牌库: 0';
		                }
						else{
							toolTiphtml = params.name + '品牌库: ' + params.value;
		                }
						//console.log(toolTiphtml)                        
		                return toolTiphtml;                   
		        }
		    },
		    toolbox: {
		        feature: {
		            saveAsImage: {}
		        }
		    },
			// 图例 
		    visualMap: {
		        show: true,
		        left: 'left',
		        top: 'bottom',
		        seriesIndex: [0],
		        type:'piecewise',
		        pieces:[
		            {min:100, color: '#ef385d'},
		            {min:40, max:100, color: '#f5a71d'},
		            {min:20, max:40, color: '#2fb1c0'},
		            {min:5, max:20, color: '#b1d236'},
		            {min:1, max:5, color: 'rgb(131,175,155)'}
		        ],            
		        textStyle: {
		            color: '#000000'
		        }
		    },            
		    geo: {
		        show: true,
		        map: mapName,
		        label: {
		            normal: {
		                show: false,
		                fontSize:12,
		            },
		            emphasis: {
		                show: false,
		            }
		        },
				zoom:1.1,//地图放大倍数
		        roam: true, //  缩放
		        itemStyle: {
		            normal: {
		                areaColor: '#F6F6F6',
		                borderColor: '#666666',
		            },
		            emphasis: {
		                areaColor: '#C10D26',
		            }
		        }
		    },
		    series: [
		        {
		            type: 'map',
		            map: mapName,
		            geoIndex: 0,           
		            animation: false,
		            data: data,
					
		        },
		    ]
		};
			
		// 使用刚指定的配置项和数据显示图表。
		myChart.setOption(option);
	}
	
}

INDEX.initial();
INDEX.wordFun();
INDEX.chinaFun();
